package com.example.lean;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


    static TextView txt;
    int num, numouter;;
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        txt = findViewById(R.id.txt);
    }
    //应用程序内部组件接收消息
    public void myClickInner(View v)
    {
        Intent intent = new Intent();
        intent.setAction("inner");//设置action属性值
        //Android 8 之后设置接收器的包名才能接受到广播
        intent.setPackage("com.example.lean");
        num++;
        String msg="内部广播信息第"+Integer.toString(num)+"次发送";
        intent.putExtra("count",msg);
        sendBroadcast(intent);//发送广播消息

        Toast.makeText(this,"发送：" + msg,Toast.LENGTH_SHORT).show();
    }
    //外部应用程序接收消息
    public void myClickOuter(View v)
    {
        Intent intent = new Intent();
        intent.setAction("outer");//设置action属性值
        //Android 8 之后设置接收器的包名才能接受到广播
        intent.setPackage("com.example.lean2");
        numouter++;
        String msg="外部广播信息第"+Integer.toString(numouter)+"次发送";
        intent.putExtra("count",msg);
        sendBroadcast(intent);//发送广播消息

        Toast.makeText(this,"发送：" + msg,Toast.LENGTH_SHORT).show();
    }
}